<?php

namespace App\Http\Controllers\Center;

use App\Http\Controllers\Controller;
use App\Http\Requests\Center\ChangePasswordRequest;
use App\Http\Requests\Center\sendVerifyLinkRequest;
use App\Models\EmailVerification;
use App\Notifications\EmailVerification as EmailVerificationNotification;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Notification;
use Illuminate\Validation\ValidationException;

class SecurityController extends Controller
{

    /**
     * 显示安全中心首页
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function showIndex()
    {
        return $this->view('center.security.index');
    }

    /**
     * 显示修改邮箱页面
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function showEmail()
    {
        return $this->view('center.security.email', [
            'user' => $this->user()
        ]);
    }

    /**
     * 发送验证邮件
     *
     * @param sendVerifyLinkRequest $request
     *
     * @return mixed
     */
    public function sendVerifyLink(sendVerifyLinkRequest $request)
    {

        $email = $request->input('email');

        $token = token();

        EmailVerification::updateOrCreate([
            'type' => EmailVerification::TYPE_EMAIL,
            'email' => $email
        ], [
            'token' => $token
        ]);

        Notification::route('mail', $email)
            ->notify(new EmailVerificationNotification($token));

        return $this->back()
            ->withSuccess('验证邮件发送成功!');
    }

    /**
     * 验证并修改邮箱
     *
     * @param $token
     *
     * @return \Illuminate\Http\RedirectResponse
     * @throws \Exception
     */
    public function emailVerify($token)
    {
        $verify = EmailVerification::where([
            'type' => EmailVerification::TYPE_EMAIL,
            'token' => $token
        ])->first();

        if ($verify && !$verify->isExpired()) {

            // 更新用户邮箱地址
            $this->user()->update([
                'email' => $verify['email']
            ]);

            // 删除验证记录
            $verify->delete();

            return $this->route('center.security.email')
                ->with('success', '邮箱验证成功');

        }

        return $this->route('center.security.email')
            ->withError('验证邮件不存在或已过期');
    }

    /**
     * 显示修改密码页面
     *
     * @return \Illuminate\View\View
     */
    public function showPassword()
    {
        return $this->view('center.security.password');
    }

    /**
     * 修改密码
     *
     * @param ChangePasswordRequest $request
     *
     * @return \Illuminate\Http\RedirectResponse
     * @throws ValidationException
     */
    public function changePassword(ChangePasswordRequest $request)
    {
        $user = $this->user();

        if (!Hash::check($request->input('old_password'), $user->getAuthPassword())) {
            $this->validationException([
                'old_password' => '旧密码不正确'
            ]);
        }

        $user->update([
            'password' => Hash::make($request->input('password'))
        ]);

        return $this->back()
            ->withSuccess('修改成功');
    }

    /**
     * 显示登录日志页面
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function showLoginLogs()
    {
        return $this->view('center.security.login-logs');
    }
}